package com.supcon.tinyportal.tags;

import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;

public class HandlerTag extends TagSupport {

	private static final long serialVersionUID = 1L;

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	private String key;

	public String getEvent() {
		return event;
	}

	public void setEvent(String event) {
		this.event = event;
	}

	private String event;

	@SuppressWarnings("unchecked")
	public int doStartTag() throws JspException {
		HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
		if (event == null && key == null) {
			if ("POST".equalsIgnoreCase(request.getMethod())) {
				return EVAL_BODY_INCLUDE;
			}
		} else {
			Map<String, String> reqParams = request.getParameterMap();
			if (key != null && event != null && reqParams.containsKey(key) && event.equals(reqParams.get(key))) {
				return EVAL_BODY_INCLUDE;
			}

			if (event != null && reqParams.containsKey(event)) {
				return EVAL_BODY_INCLUDE;
			}
		}
		return SKIP_BODY;
	}
}
